home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 79 / maccd 79.iso / multimedial / GL Tron / Source / gltron / load_texture.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-07-10  |  1.9 KB  |  72 lines  |  [TEXT/CWIE]

  1. #include "gltron.h"
  2.  
  3. void freeTextureData(texture *tex) {
  4.   free(tex->data);
  5.   free(tex);
  6. }
  7.  
  8. texture* loadTextureData(char *filename) {
  9.   texture *tex;
  10.   char *path;
  11.  
  12.   path = getFullPath(filename);
  13.   if(path != NULL)
  14.     tex = LOAD_TEX(path);
  15.   else {
  16.     fprintf(stderr, "fatal: could not load %s, exiting...\n", filename);
  17.     exit(1);
  18.   }
  19.  
  20.   if(tex == NULL) {    
  21.     fprintf(stderr, "fatal: failed load %s, exiting...\n", filename);
  22.     exit(1);
  23.   }
  24.   return tex;
  25. }
  26.  
  27. void loadTexture(char *filename, int format) {
  28.   texture *tex;
  29.   GLint internal;
  30.  
  31.   tex = loadTextureData(filename);
  32.   if(tex->channels == 3) internal = GL_RGB;
  33.   else internal = GL_RGBA;
  34.   if(format == GL_DONT_CARE) {
  35.     if(tex->channels == 3) format = GL_RGB;
  36.     if(tex->channels == 4) format = GL_RGBA;
  37.   }
  38.   /* TODO: build mipmaps the proper way, box filters suck */
  39.   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  40.  
  41.   if(game->settings->use_mipmaps) {
  42.     texture *newtex;
  43.     int level = 0;
  44.  
  45.     glTexImage2D(GL_TEXTURE_2D, level, format, tex->width, tex->height,
  46.          0, internal, GL_UNSIGNED_BYTE, tex->data);
  47.     while (tex->width > 1 || tex->height > 1) {
  48.       newtex = mipmap_png_texture(tex, 1, 0, 0);
  49.       freeTextureData(tex);
  50.       tex = newtex;
  51.       level++;
  52.       fprintf(stderr, "creating mipmap level %d, size(%d, %d)\n", 
  53.           level, tex->width, tex->height);
  54.       glTexImage2D(GL_TEXTURE_2D, level, format, tex->width, tex->height,
  55.            0, internal, GL_UNSIGNED_BYTE, tex->data);
  56.     }
  57.   } else { 
  58.       glTexImage2D(GL_TEXTURE_2D, 0, format, tex->width, tex->height, 0,
  59.            internal, GL_UNSIGNED_BYTE, tex->data);
  60.   }
  61.     /*
  62.   if(game->settings->use_mipmaps) {
  63.     gluBuild2DMipmaps(GL_TEXTURE_2D, format, tex->width, tex->height, 
  64.               internal, GL_UNSIGNED_BYTE, tex->data);
  65.   } else { 
  66.     glTexImage2D(GL_TEXTURE_2D, 0, format, tex->width, tex->height, 0,
  67.          internal, GL_UNSIGNED_BYTE, tex->data);
  68.   }
  69.     */
  70.   freeTextureData(tex);
  71. }
  72.